<?php
/**
 * 简化的客户端代码
 * Class CommandFactory
 */
class CommandFactory{
	private static $dir = '/data/www/study_node/PHP/deep/demo8/command';

	static function getCommand($action = "Default"){
		if(preg_match("/\W/",$action)){
			throw new Exception("illegal characters in action");
		}
		$class = UCFirst(strtolower($action))."Command";
		$file = self::$dir.DIRECTORY_SEPARATOR."{$class}.php";

		if(!file_exists($file)){
			throw new CommandNotFoundException("could not find '$file'");
		}

		require($file);
		if(!class_exists($class)){
			throw new CommandNotFoundException("no '$class' class located");
		}
		$cmd = new $class();
		return $cmd;
	}
}